<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix='sec' uri='http://www.springframework.org/security/tags'%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
	<script type="text/javascript" src="scripts/boot.js"></script>
	<style type="text/css">
    .errorText
    {
        color:red;
    }
    </style>
</head>

<body>
	<form id="form1" method="post">
		<p style="margin-left: 20px;"><label>请输入类别名称：</label></p>
		<p style="margin-left: 20px;">
			<input id="textbox1"  name="name" class="mini-textbox" required="true" errorMode="none" onvalidation="checkValidation" requiredErrorText="类别名称不能为空" required="true" vtype="rangeLength:0,50" style="width: 180px;"/>
		</p>
		<p style="margin-left: 20px;"><span id="name_error" class="errorText" ></span></p>
		<input id="text1"  name="id" class="mini-hidden" required="true" />
	</form>
    <p style="text-align: center;margin-top: 55px">
    	<a class="mini-button" onclick="onOk" style="width:60px;margin-right:20px;">确定</a>
    	<a class="mini-button" onclick="onCancel" style="width:60px;">取消</a>
    </p>
</body>

<script type="text/javascript">
	var nodeid = "";
	var oldName = "";
	mini.parse();
	var form = new mini.Form("form1");
	
	function SaveData() {
        var o = form.getData();            

        form.validate();
        if (form.isValid() == false) return;
      	
        //验证输入的名称是否已经存在
        var tempName = mini.get("textbox1").getFormValue();
      	if(oldName == tempName){
      		//名称没有改变
      		window.CloseOwnerWindow("succ");
      	}else{
      		$.post("medicine_sort_repeat_name.do",{name:tempName,pid:nodeid},function(data){
            	if(data.result){
            		mini.confirm("已在该类别下找到重复的类别名，确定继续添加？", "确定？",function (action) {
            			if (action == "ok") {
            				 $.ajax({
            			            url: "medicine_sort_save_edit.do",
            						type: 'post',
            						dataType: 'json',
            			            data: o,
            			            cache: false,
            			            success: function (text) {
            			            	if(text.success == 'true'){
            				            	window.CloseOwnerWindow("succ");
            				            }else if(text.success == 'false'){
            				            	mini.alert("id不能为空！");
            				            }else {
            				            	mini.alert("数据错误");
            				            }
            			            },
            			            error: function (jqXHR, textStatus, errorThrown) {
            			                alert(jqXHR.responseText);
            			                CloseWindow();
            			            }
            			        });
            			}
            		});
            	}else{
            		$.ajax({
			            url: "medicine_sort_save_edit.do",
						type: 'post',
						dataType: 'json',
			            data: o,
			            cache: false,
			            success: function (text) {
			            	if(text.success == 'true'){
				            	window.CloseOwnerWindow("succ");
				            }else if(text.success == 'false'){
				            	mini.alert("id不能为空！");
				            }else {
				            	mini.alert("数据错误");
				            }
			            },
			            error: function (jqXHR, textStatus, errorThrown) {
			                alert(jqXHR.responseText);
			                CloseWindow();
			            }
			        });
            	}
            },"json");
      	}
    }
	
	function onOk(e) {
        SaveData();
    }
	
	function SetData(data){
	     data = mini.clone(data);
	     form.setData(data);
	     nodeid = data.pid;
	     oldName = data.name;
	}
	
	function CloseWindow(action) {
        if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
        else window.close();            
    }
	
	function onCancel(e) {
        CloseWindow("cancel");
    }

   	function updateError(e) {
	    var id =e.sender.name + "_error";
	    var el = document.getElementById(id);
	    if (el) {
	        el.innerHTML =e.errorText;
	    }
   	}
   
   	function checkValidation(e) {                  
  		updateError(e);
   	}
	
</script>